package com.winchain.provider;

import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.MoneyUtil;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.base.utils.SystemUtil.SystemMode;
import com.shell.customer.feign.CustomerFeign;
import com.shell.customer.po.CusRec;
import com.shell.feign.PubParameterFeign;
import com.shell.framework.utils.MessageSender;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.message.keyword.ActivityStartKeyWord;
import com.shell.governor.modules.vo.ParameterVo;
import com.winchain.chainbill.basic.activity.po.BasAct;
import com.winchain.chainbill.basic.activity.service.BasActGrpService;
import com.winchain.chainbill.basic.activity.service.BasActService;
import com.winchain.chainbill.constant.ActivityConstants;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ActivityReminderProvider {
	@Autowired
	private PubParameterFeign pubParameterFeign;
	@Autowired
	private BasActService basActService;
	@Autowired
	private CustomerFeign customerFeign;
	@Autowired
	private BasActGrpService basActGrpService;
	private static Map<String, String> cpnTypeMap = new HashMap();

	static {
		try {
			Map<String, Map<String, String>> constantMap = SystemUtil
					.getConstantsCnNameValueMap(ActivityConstants.class);

			Map<String, String> tmpMap;
			tmpMap = (Map) constantMap.get("CpnType");
			for (String key : tmpMap.keySet()) {
				cpnTypeMap.put(tmpMap.get(key), key);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping({ "/activityReminder/remind" })
	public void remind() {
		String workDate = getWorkDate();
		String url = SpringContextUtil.getProperty("chainbill.website");
		if (StringUtil.emptyAndNull(url)) {
			throw new UnCheckMsgException("��������������");
		}
		String tagCodes = "MEMBER,SUPPLIER";

		BasAct tmp = new BasAct();
		tmp.setStrtDate(workDate);
		tmp.setActSts("1");
		List<BasAct> actList = this.basActService.selectByEntity(tmp);
		ActivityStartKeyWord keyWord = null;
		for (BasAct act : actList) {
			keyWord = getKeyWord(act);

			String[] grpIds = this.basActGrpService.selectGrpIdsByActId(act.getActId());
			for (String grpId : grpIds) {
				List<CusRec> cusRecList = this.customerFeign.getCusyGroupIdAndTagCodes(grpId, tagCodes);
				for (CusRec cusRec : cusRecList) {
					MessageSender.sendMessageByTmpl("-1", "MT08002", keyWord, cusRec

							.getCusId());
				}
			}
		}
	}

	private ActivityStartKeyWord getKeyWord(BasAct act) {
		String cpnAmt = MoneyUtil.format(act.getCpnAmt());
		String cpnType = (String) cpnTypeMap.get(act.getCpnType());
		String endDate = SysDateUtil.parseDate(act.getEndDate(), "yyyyMMdd", "yyyy��M��d��");
		CusRec issuer = this.customerFeign.selectCusById(act.getIssuer());
		ActivityStartKeyWord keyWord = new ActivityStartKeyWord();
		keyWord.setCpnAmt(cpnAmt);
		keyWord.setEndDate(endDate);
		keyWord.setCpnType(cpnType);
		keyWord.setIssuer(issuer.getCusName());
		keyWord.setUrl(SpringContextUtil.getProperty("chainbill.website"));
		return keyWord;
	}

	private String getWorkDate() {
		String workDate = SysDateUtil.getSystemDate();
		if (!SystemUtil.SystemMode.prod.equals(SystemUtil.getSystemMode())) {
			ParameterVo parameter = this.pubParameterFeign.getCommonParameter("SYS_DATE_CONF");
			if ((parameter != null) && (!StringUtil.emptyAndNull(parameter.getParmValue()))) {
				workDate = parameter.getParmValue();
			}
		}
		return workDate;
	}
}
